package com.zhongwang.mms.module.jiaGongSemi.controller;

import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.gen.entity.User;
import com.zhongwang.mms.module.half.model.HInBillModel;
import com.zhongwang.mms.module.half.model.HInventoryQueryModel;
import com.zhongwang.mms.module.half.model.HOrderDetailModel;
import com.zhongwang.mms.module.half.service.FinishInWarService;
import com.zhongwang.mms.module.half.service.HBasicService;
import com.zhongwang.mms.module.half.service.OutOfBuyWarService;
import com.zhongwang.mms.module.jiaGongSemi.model.JSemiDetail;
import com.zhongwang.mms.module.jiaGongSemi.model.JSemiInBillModel;
import com.zhongwang.mms.module.jiaGongSemi.model.JSemiInventory;
import com.zhongwang.mms.module.jiaGongSemi.service.SemiOutService;
import com.zhongwang.mms.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 外购/外协出库
 */
@Controller
@RequestMapping("/semiOut")
public class SemiOutController extends BaseController {
    @Autowired
    private OutOfBuyWarService outOfBuyWarService;
    @Autowired
    private FinishInWarService finishInWarService;
    @Autowired
    private HBasicService HBasicService;
    @Autowired
    private SemiOutService semiOutService;


    // 创建入库单页面
    @GetMapping("/page")
    public String create(Model model) {

        // 获取登入用户实体
        User user = ShiroUtils.getUser();
        model.addAttribute("userName", user.getName());
        model.addAttribute("dept", HBasicService.getDept());
        model.addAttribute("odrDate", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        return "jiaGongSemi/semiOut/page";
    }
    // 库存列表
    @ResponseBody
    @PostMapping("/list")
    public JsonResult stockList(JSemiInventory query){

        startPage();
        List<JSemiDetail> list = semiOutService.outOfFinishWar(query);
        return jsonResult(list);
    }

    /**
     * 创建外购外协件出库单保存
     * @param jSemiInBillModel
     * @return
     */
    @ResponseBody
    @PostMapping("/create")
    public JsonResult create(@RequestBody JSemiInBillModel jSemiInBillModel) {

        return semiOutService.saveHInBillModel(jSemiInBillModel);
    }

    /**
     * 打印获取主单信息
     * @param id
     * @return
     */
    @ResponseBody
    @PostMapping("/printList")
    public JsonResult printList(@RequestParam("id") Integer id){

        return semiOutService.selectHOrderById(id);

    }

    /**
     * 打印获取明细信息
     * @param id
     * @return
     */
    @ResponseBody
    @PostMapping("/printDetailList")
    public JsonResult printDetailList(@RequestParam("id") Integer id){

        return semiOutService.selectHOrderDetailByOdrId(id);

    }
}
